package com.example.demo2;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class HTTPCLientDemo {
    public static void main(String[] args) throws Exception{

        Runnable runnable = ()->{
            try {
            List<NameValuePair> formparams = new ArrayList<NameValuePair>();
                double random = Math.ceil(Math.random()*1000);
                formparams.add(new BasicNameValuePair("_id", ""+ random));
            HttpEntity reqEntity = new UrlEncodedFormEntity(formparams, "utf-8");

            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(5000)//一、连接超时：connectionTimeout-->指的是连接一个url的连接等待时间
                    .setSocketTimeout(5000)// 二、读取数据超时：SocketTimeout-->指的是连接上一个url，获取response的返回等待时间
                    .setConnectionRequestTimeout(5000)
                    .build();

            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://localhost:8080/getapi1");
            post.setEntity(reqEntity);
            post.setConfig(requestConfig);
            HttpResponse response = client.execute(post);

            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = response.getEntity();
                String message = EntityUtils.toString(resEntity, "utf-8");
                System.out.println(message);
            } else {
                System.out.println("请求失败");
            }
            }catch (Exception e){

            }
        };

        for (int i = 0; i < 1000; i++) {
            new Thread(runnable).start();
        }

        Thread.sleep(990000);





    }
}
